package com.lishe.lishe.lishe.module.mall.view;

import android.animation.Animator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.support.annotation.IntDef;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lishe.lishe.R;


/**
 * Created by leon on 1/28/16.
 */
public class SearchBarView extends RelativeLayout {

    static final String TAG = SearchBarView.class.getSimpleName();
    private final boolean in = true;
    //  @BindView(R.id.internal_iv_search_icon)
    LinearLayout mInternalIvSearchIcon;
    //  @BindView(R.id.internal_rv_holder)
    RelativeLayout mInternalRvHolder;
    //  @BindView(R.id.internal_vs_cancel)
    ViewSwitcher mInternalVsCancel;

    //  @BindView(R.id.internal_iv_clear)
    ImageView mInternalIvClear;
    //  @BindView(R.id.internal_et_search)
    EditText mInternalEtSearch;
    private float upDimen = 0f;
    private InputMethodManager imm;

    public SearchBarView(Context context) {
        this(context, null);
    }

    public SearchBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta =
                attrs == null ? null : getContext().obtainStyledAttributes(attrs, R.styleable.SearchBar);
        if (ta != null) {
            upDimen = ta.getDimension(R.styleable.SearchBar_sb_float_height, upDimen);
            ta.recycle();
        }
        init();

    }

    public void requestEditFocus() {

        Log.e(TAG,"requestEditFocus");
        //mInternalEtSearch.clearFocus(); //失去焦点

        //mInternalEtSearch.requestFocus();//获取焦点
        mInternalEtSearch.setFocusable(true);
        mInternalEtSearch.setFocusableInTouchMode(true);
        mInternalEtSearch.requestFocus();
        ((InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mInternalEtSearch,InputMethodManager.SHOW_IMPLICIT);


    }


    public EditText getEditTextSearch() {
        return mInternalEtSearch;
    }

    //  @OnClick(R.id.internal_iv_clear)
    void clearText() {
        mInternalEtSearch.getText().clear();//使用clear，再次点击时：不显示光标；输入时：显示光标。
        mInternalEtSearch.setText(" ");//临时解决办法.
        //mInternalEtSearch.setText("");
    }

    public void setOnButton(final Runnable runnable) {
        mInternalVsCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e(TAG,"mInternalVsCancel.onClick");
                clearText();
                InputMethodManager imm = (InputMethodManager) getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

//                ((InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromInputMethod(mInternalEtSearch.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//                mInternalEtSearch.setFocusable(false);
//                mInternalEtSearch.setFocusableInTouchMode(false);
//                mInternalEtSearch.clearFocus();
                post(runnable);
//                mInternalVsCancel.showCancel(false, new Animator.AnimatorListener() {
//                    @Override
//                    public void onAnimationStart(Animator animation) {
//
//                    }
//
//                    @Override
//                    public void onAnimationEnd(Animator animation) {
//                        post(runnable);
//                    }
//
//                    @Override
//                    public void onAnimationCancel(Animator animation) {
//                    }
//
//                    @Override
//                    public void onAnimationRepeat(Animator animation) {
//
//                    }
//                });
            }
        });
    }

    private void init() {
        //insert first view into the Framlayout,, and return `root view`(this)
        inflate(getContext(), R.layout.internal_searchbar, this);

        findViewById(R.id.internal_iv_clear).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                clearText();
            }
        });

        this.isInEditMode();
        mInternalIvSearchIcon = (LinearLayout) findViewById(R.id.internal_iv_search_icon);
        mInternalRvHolder = (RelativeLayout) findViewById(R.id.internal_rv_holder);
        mInternalVsCancel = (ViewSwitcher) findViewById(R.id.internal_vs_cancel);
        mInternalIvClear = (ImageView) findViewById(R.id.internal_iv_clear);
        mInternalEtSearch = (EditText) findViewById(R.id.internal_et_search);
        mInternalEtSearch.setLines(1);
        mInternalEtSearch.setInputType(InputType.TYPE_CLASS_TEXT);
        mInternalEtSearch.setCursorVisible(true);

        //ButterKnife.bind(this);
        /**
         * -root
         * --RelevantLayout(RealSearch) (view[0])
         */
        mInternalEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(final Editable s) {
                if (s.length() == 0) {
                    mInternalIvClear.setVisibility(GONE);
                } else {
                    mInternalIvClear.setVisibility(VISIBLE);
                }
            }
        });



        imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        mInternalEtSearch.setFocusable(true);
        mInternalEtSearch.requestFocus();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (imm != null) {
            imm.hideSoftInputFromWindow(mInternalEtSearch.getWindowToken(), 0);
        }
    }

    public boolean isClosed() {
        return in;
    }

    //------------------我的改动---------------------

    @Override
    public void setVisibility( int visibility) {
        super.setVisibility(visibility);
    }
}
